package com.hongyuan.venue.ui.home.view_mode;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableList;

import com.hongyuan.venue.R;
import com.hongyuan.venue.base.CustomViewModel;
import com.hongyuan.venue.data.Constants;
import com.hongyuan.venue.data.MyRepository;
import com.hongyuan.venue.ui.home.beans.TrainBeans;

import java.util.List;

import me.tatarka.bindingcollectionadapter2.BR;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class TrainListViewModel extends CustomViewModel<MyRepository> {

    public TrainListViewModel(@NonNull Application application, MyRepository model) {
        super(application, model);
    }

    //给RecyclerView添加ObservableList
    public ObservableList<TrainItemViewModel> observableList = new ObservableArrayList<>();

    //给RecyclerView添加ItemBinding
    public ItemBinding<TrainItemViewModel> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_train_list);

    @Override
    protected void returnData(int code, Object dataBean) {
        super.returnData(code, dataBean);

        if(code == Constants.COURSE_TRAIN_LIST){
            List<TrainBeans.ListBean> list = ((TrainBeans)dataBean).getList();
            //清除旧数据
            if(curPage == FIRST_PAGE){
                observableList.clear();
            }

            if(list != null && list.size() > 0){
                for(TrainBeans.ListBean bean : list){
                    TrainItemViewModel itemViewModel = new TrainItemViewModel(this,bean);
                    observableList.add(itemViewModel);
                }
            }

            if(observableList.size() > 0){
                if(list == null || list.size() == 0){
                    getUC().getFinishLoadMoreData().call();
                }
            }else{

            }
        }
    }
}
